package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.ReportFieldConfigMapper;
import com.ruoyi.system.domain.ReportFieldConfig;
import com.ruoyi.system.service.IReportFieldConfigService;

/**
 * 报表字段配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-23
 */
@Service
public class ReportFieldConfigServiceImpl implements IReportFieldConfigService 
{
    @Autowired
    private ReportFieldConfigMapper reportFieldConfigMapper;

    /**
     * 查询报表字段配置
     * 
     * @param id 报表字段配置主键
     * @return 报表字段配置
     */
    @Override
    public ReportFieldConfig selectReportFieldConfigById(Long id)
    {
        return reportFieldConfigMapper.selectReportFieldConfigById(id);
    }

    /**
     * 查询报表字段配置列表
     * 
     * @param reportFieldConfig 报表字段配置
     * @return 报表字段配置
     */
    @Override
    public List<ReportFieldConfig> selectReportFieldConfigList(ReportFieldConfig reportFieldConfig)
    {
        return reportFieldConfigMapper.selectReportFieldConfigList(reportFieldConfig);
    }

    /**
     * 根据报表编码查询字段配置列表
     * 
     * @param reportCode 报表编码
     * @return 字段配置集合
     */
    @Override
    public List<ReportFieldConfig> selectFieldConfigByReportCode(String reportCode)
    {
        return reportFieldConfigMapper.selectFieldConfigByReportCode(reportCode);
    }

    /**
     * 根据报表编码查询列表字段配置
     * 
     * @param reportCode 报表编码
     * @return 字段配置集合
     */
    @Override
    public List<ReportFieldConfig> selectListFieldsByReportCode(String reportCode)
    {
        List<ReportFieldConfig> allFields = reportFieldConfigMapper.selectFieldConfigByReportCode(reportCode);
        return allFields.stream()
                .filter(f -> "1".equals(f.getIsList()))
                .sorted((a, b) -> a.getListSort().compareTo(b.getListSort()))
                .collect(Collectors.toList());
    }

    /**
     * 根据报表编码查询查询字段配置
     * 
     * @param reportCode 报表编码
     * @return 字段配置集合
     */
    @Override
    public List<ReportFieldConfig> selectQueryFieldsByReportCode(String reportCode)
    {
        List<ReportFieldConfig> allFields = reportFieldConfigMapper.selectFieldConfigByReportCode(reportCode);
        return allFields.stream()
                .filter(f -> "1".equals(f.getIsQuery()))
                .sorted((a, b) -> a.getQuerySort().compareTo(b.getQuerySort()))
                .collect(Collectors.toList());
    }

    /**
     * 新增报表字段配置
     * 
     * @param reportFieldConfig 报表字段配置
     * @return 结果
     */
    @Override
    public int insertReportFieldConfig(ReportFieldConfig reportFieldConfig)
    {
        reportFieldConfig.setCreateTime(DateUtils.getNowDate());
        return reportFieldConfigMapper.insertReportFieldConfig(reportFieldConfig);
    }

    /**
     * 修改报表字段配置
     * 
     * @param reportFieldConfig 报表字段配置
     * @return 结果
     */
    @Override
    public int updateReportFieldConfig(ReportFieldConfig reportFieldConfig)
    {
        reportFieldConfig.setUpdateTime(DateUtils.getNowDate());
        return reportFieldConfigMapper.updateReportFieldConfig(reportFieldConfig);
    }

    /**
     * 批量删除报表字段配置
     * 
     * @param ids 需要删除的报表字段配置主键
     * @return 结果
     */
    @Override
    public int deleteReportFieldConfigByIds(Long[] ids)
    {
        return reportFieldConfigMapper.deleteReportFieldConfigByIds(ids);
    }

    /**
     * 删除报表字段配置信息
     * 
     * @param id 报表字段配置主键
     * @return 结果
     */
    @Override
    public int deleteReportFieldConfigById(Long id)
    {
        return reportFieldConfigMapper.deleteReportFieldConfigById(id);
    }

    /**
     * 批量保存字段配置
     * 
     * @param reportCode 报表编码
     * @param fieldConfigs 字段配置列表
     * @return 结果
     */
    @Override
    @Transactional
    public int batchSaveFieldConfig(String reportCode, List<ReportFieldConfig> fieldConfigs)
    {
        // 先删除原有配置
        reportFieldConfigMapper.deleteReportFieldConfigByReportCode(reportCode);
        
        // 批量插入新配置
        if (fieldConfigs != null && !fieldConfigs.isEmpty())
        {
            for (ReportFieldConfig config : fieldConfigs)
            {
                config.setReportCode(reportCode);
                config.setCreateTime(DateUtils.getNowDate());
            }
            return reportFieldConfigMapper.batchInsertReportFieldConfig(fieldConfigs);
        }
        return 0;
    }
}
